package br.great.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import br.great.beans.Wiki;

/**
* PhotoMap
* Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
*/

/**
 * @author Developpement of PhotoMap
 */
public class DAO_Wiki {
    private int j;
    private static volatile DAO_Wiki instaceDAO_Wiki = new DAO_Wiki();
    
    public static int counterWiki =0;
    /** The private constructor of DAO_Collection for singleton
    */
    private DAO_Wiki(){    
    }
    
    /** The method for get the instance of the class DAO_Collection
    * @return DAO_Collection instaceDAO_Collection
    */
    public static DAO_Wiki getInstance() {
        //if(instaceDAO_Collection==null)
           synchronized(DAO_Wiki.class) {
             if (instaceDAO_Wiki == null)
               instaceDAO_Wiki = new DAO_Wiki();
           }
        return instaceDAO_Wiki;
    }
      
    public void insertWiki(int ID_Multimedia, Wiki wiki){
        counterWiki++;
        //System.out.println(wiki.getTitle()+counterWiki);
        // get tag for looking for the Wiki is existe or not
        String getWiki = "select * from wiki_object where Lower(name)=Lower('"+wiki.getTitle()+"');";
        // String createSeqWiki = "create sequence seqmax_Wiki increment by 1;";
        PgConnect conn = PgConnect.getInstance();
        try {
            ResultSet rs_getWiki = conn.executeQuery(getWiki);
            // if the tag is in the table
                if(rs_getWiki.next()){
                    int id_GEO_REF = rs_getWiki.getInt("ID_GEO_REFERENCED_OBJECT");
                    String insertPHOTO_SPATIALRELATIONS = "insert into PHOTO_SPATIALRELATIONS values("+id_GEO_REF+","+ID_Multimedia+",'"+wiki.getRELATION_TYPE()+"','"+wiki.getDistance()+"');";
                    conn.executeInsert(insertPHOTO_SPATIALRELATIONS);
                }else{
                // if the wiki isn't in table, we insert the wiki in it
                String insertGEO_REFERENCED_OBJECT = "insert into GEO_REFERENCED_OBJECT values(nextval('seqmax_GeoRef'),'POINT("+wiki.getLatitude()+" "+wiki.getLongitude()+")');";
                conn.executeInsert(insertGEO_REFERENCED_OBJECT);
                String getIdGeoRef = "select max(ID_GEO_REFERENCED_OBJECT) from GEO_REFERENCED_OBJECT";
                ResultSet rs_IdGeoRef = conn.executeQuery(getIdGeoRef);
                while (rs_IdGeoRef.next()) {
                    j = rs_IdGeoRef.getInt("max");
                }

                String insertWiki = "insert into wiki_object values("+j+",'"+wiki.getTitle()+"','"+wiki.getWikipediaUrl()+"');";
                conn.executeInsert(insertWiki);
                String insertPHOTO_SPATIALRELATIONS = "insert into PHOTO_SPATIALRELATIONS values("+j+","+ID_Multimedia+",'"+wiki.getRELATION_TYPE()+"','"+wiki.getDistance()+"');";
                conn.executeInsert(insertPHOTO_SPATIALRELATIONS);
            }
        } catch (SQLException ex) {
            System.err.println("error of Wiki");
            ex.getStackTrace();
        }
        conn.Close();
    }
//    public static void main(String[] args){
//        DAO_Wiki dw = DAO_Wiki.getInstance();
//        Wiki wiki = new Wiki();
//        wiki.setLatitude(-126.49);
//        wiki.setLongitude(45.39);
//        wiki.setTitle("wiki02");
//        wiki.setFeature("place");
//        wiki.setRELATION_TYPE("proche");
//        wiki.setWikipediaUrl("org.wiki01");
//        dw.insertWiki(4, wiki);
//    }
}
